package com.jyxd.web.service.assessment;

import com.jyxd.web.dao.assessment.TemperatureDataDao;
import com.jyxd.web.data.assessment.TemperatureData;
import com.jyxd.web.data.dto.HttpResult;
import com.jyxd.web.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

import static com.jyxd.web.util.ExecuteUtil.errorIfEmpty;

@Service
public class TemperatureDataService {

    @Autowired
    private TemperatureDataDao temperatureDataDao;

    /**
     * 新增或修改体温单记录
     *
     * @param temperatureData
     * @return boolean
     */
    public HttpResult insert (TemperatureData temperatureData) {
        errorIfEmpty(temperatureData, "体温数据对象不能为空");
        errorIfEmpty(temperatureData.getPatientId(), "患者id不能为空");
        errorIfEmpty(temperatureData.getPageNumber(), "数据页码不能为空");
        errorIfEmpty(temperatureData.getSerialNumber(), "数据序号不能为空");
        errorIfEmpty(temperatureData.getTemperatureType(), "数据类型不能为空");
        Map<String, Object> map = new HashMap<>();
        map.put("patientId", temperatureData.getPatientId());
        map.put("pageNumber", temperatureData.getPageNumber());
        map.put("serialNumber", temperatureData.getSerialNumber());
        map.put("temperatureType", temperatureData.getTemperatureType());
        TemperatureData queryOne = temperatureDataDao.queryOne(map);
        if (Objects.isNull(queryOne)) {
            temperatureData.setId(UUIDUtil.getUUID());
            temperatureData.setCreateTime(new Date());
            temperatureData.setStatus(1);
            temperatureDataDao.insert(temperatureData);
            return new HttpResult(200, "数据新增成功");
        }
        queryOne.setTemperatureValue(temperatureData.getTemperatureValue());
        temperatureDataDao.update(queryOne);
        return new HttpResult(200, "数据修改成功");
    }

    public Map<String, Object>  getDataList (Map<String, Object> requestMap) {
        errorIfEmpty(requestMap, "体温数据对象不能为空");
        errorIfEmpty(requestMap.get("patientId"), "患者id不能为空");
        errorIfEmpty(requestMap.get("pageNumber"), "数据页码不能为空");
        String patientId = (String) requestMap.get("patientId");
        int pageNumber = (Integer) requestMap.get("pageNumber");
        Map<String, Object> map = new HashMap<>();
        map.put("code", getDataList(42, "code", patientId, pageNumber));
        map.put("code1", getDataList(7, "code1", patientId, pageNumber));
        map.put("code2", getDataList(7, "code2", patientId, pageNumber));
        map.put("code3", getDataList(7, "code3", patientId, pageNumber));
        map.put("code4", getDataList(7, "code4", patientId, pageNumber));
        map.put("code5", getDataList(7, "code5", patientId, pageNumber));
        map.put("code6", getDataList(14, "code6", patientId, pageNumber));
        map.put("code7", getDataList(7, "code7", patientId, pageNumber));
        map.put("code8", getDataList(7, "code8", patientId, pageNumber));
        map.put("code9", getDataList(7, "code9", patientId, pageNumber));
        map.put("code10", getDataList(7, "code10", patientId, pageNumber));
        map.put("code11", getDataList(7, "code11", patientId, pageNumber));
        map.put("code12", getDataList(7, "code12", patientId, pageNumber));
        map.put("code13", getDataList(7, "code13", patientId, pageNumber));
        map.put("code14", getDataList(7, "code14", patientId, pageNumber));
        map.put("code15", getDataList(7, "code15", patientId, pageNumber));
        map.put("code16", getDataList(7, "code16", patientId, pageNumber));
        return map;
    }

    private String[] getDataList (int size, String type, String patientId, int pageNumber) {
        String[] strList = new String[ size ];
        Arrays.fill(strList, "");
        Map<String, Object> map = new HashMap<>();
        map.put("patientId", patientId);
        map.put("pageNumber", pageNumber);
        map.put("temperatureType", type);
        List<TemperatureData> temperatureDataList = temperatureDataDao.queryList(map);
        if (CollectionUtils.isEmpty(temperatureDataList)) {
            return strList;
        }
        temperatureDataList.forEach(temperatureData -> {
            strList[ temperatureData.getSerialNumber() ] = temperatureData.getTemperatureValue();
        });
        return strList;
    }
}
